package com.flowable.service.message;


import com.common.core.domain.entity.ProcessConfig;
import com.common.core.service.ProcessConfigService;
import com.common.msg.entity.SysMessageBo;
import com.common.msg.entity.SysMsgRuleVo;
import com.common.msg.service.SysMessageService;
import com.flowable.common.enums.message.BpmMessageEnum;
import com.flowable.domain.vo.task.NeedTodo;
import com.flowable.mapper.FlowDeployMapper;
import com.flowable.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
import com.flowable.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
import com.flowable.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import com.flowable.service.message.dto.BpmMessageSendWhenTaskTimeoutReqDTO;
import com.see.utils.SseMessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BPM 消息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class BpmMessageServiceImpl implements BpmMessageService {
    @Autowired
    ProcessConfigService processConfigService;

    @Resource
    private FlowDeployMapper flowDeployMapper;

    @Autowired
    SysMessageService  sysMessageService;

    @Override
    public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
        NeedTodo needTodo= flowDeployMapper.getBusinessByProCig(reqDTO.getProcessInstanceId());
        if(needTodo!=null) {
            Map<String, String> templateParams = new HashMap<>();
            templateParams.put("moduleName", needTodo.getModuleName());
            templateParams.put("bizName", needTodo.getName());
            templateParams.put("changeName", reqDTO.getStartUserNickname());
            templateParams.put("checkResult", "同意");
            templateParams.put("checkInfo", reqDTO.getReason());
            SysMsgRuleVo sysMsgRuleVo = new SysMsgRuleVo();
            sysMsgRuleVo.setName("审核通知");
            sysMsgRuleVo.setReplacements(templateParams);
            String msgcontent = sysMessageService.replacePlaceholders(sysMsgRuleVo);
            //templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
            //templateParams.put("code",BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode());
            //SseMessageUtils.sendMessage(reqDTO.getAssigneeUserId(),templateParams.toString());
            SysMessageBo sysMessageBo = new SysMessageBo();
            sysMessageBo.setContent(msgcontent);
            sysMessageBo.setIsRead("2");
            sysMessageBo.setTitle("审核通知");
            sysMessageBo.setRevicerId(reqDTO.getStartUserId().toString());
            sysMessageService.insertSysMessage(sysMessageBo);
            SseMessageUtils.sendMessage(reqDTO.getStartUserId(), msgcontent);
        }
    }

    @Override
    public void sendMessageWhenProcessInstanceReject(BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO) {
        NeedTodo needTodo= flowDeployMapper.getBusinessByProCig(reqDTO.getProcessInstanceId());
        if(needTodo!=null) {
            Map<String, String> templateParams = new HashMap<>();
            templateParams.put("moduleName", needTodo.getModuleName());
            templateParams.put("bizName", needTodo.getName());
            templateParams.put("changeName", reqDTO.getStartUserNickname());
            templateParams.put("checkResult", "不同意");
            templateParams.put("checkInfo", reqDTO.getReason());
            SysMsgRuleVo sysMsgRuleVo = new SysMsgRuleVo();
            sysMsgRuleVo.setName("审核通知");
            sysMsgRuleVo.setReplacements(templateParams);
            String msgcontent = sysMessageService.replacePlaceholders(sysMsgRuleVo);
            //templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
            //templateParams.put("code",BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode());
            //SseMessageUtils.sendMessage(reqDTO.getAssigneeUserId(),templateParams.toString());
            SysMessageBo sysMessageBo = new SysMessageBo();
            sysMessageBo.setContent(msgcontent);
            sysMessageBo.setIsRead("2");
            sysMessageBo.setTitle("审核通知");
            sysMessageBo.setRevicerId(reqDTO.getStartUserId().toString());
            sysMessageService.insertSysMessage(sysMessageBo);
            SseMessageUtils.sendMessage(reqDTO.getStartUserId(), msgcontent);
        }
//        Map<String, Object> templateParams = new HashMap<>();
//        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
//        templateParams.put("reason", reqDTO.getReason());
//        //templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
//        templateParams.put("code",BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode());
//        SseMessageUtils.sendMessage(reqDTO.getStartUserId(),templateParams.toString());
//        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
//                BpmMessageEnum.PROCESS_INSTANCE_REJECT.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        NeedTodo needTodo= flowDeployMapper.getBusinessByProCig(reqDTO.getProcessInstanceId());
       if(needTodo!=null) {
           Map<String, String> templateParams = new HashMap<>();
           templateParams.put("moduleName", needTodo.getModuleName());
           templateParams.put("bizName", needTodo.getName());
           templateParams.put("startUserNickname", reqDTO.getStartUserNickname());
           SysMsgRuleVo sysMsgRuleVo = new SysMsgRuleVo();
           sysMsgRuleVo.setName("待办提醒");
           sysMsgRuleVo.setReplacements(templateParams);
           String msgcontent = sysMessageService.replacePlaceholders(sysMsgRuleVo);
           //templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
           //templateParams.put("code",BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode());
           //SseMessageUtils.sendMessage(reqDTO.getAssigneeUserId(),templateParams.toString());
           SysMessageBo sysMessageBo = new SysMessageBo();
           sysMessageBo.setContent(msgcontent);
           sysMessageBo.setIsRead("2");
           sysMessageBo.setTitle("待办提醒");
           sysMessageBo.setRevicerId(reqDTO.getAssigneeUserId().toString());
           sysMessageService.insertSysMessage(sysMessageBo);
           SseMessageUtils.sendMessage(reqDTO.getAssigneeUserId(), msgcontent);
       }
//        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getAssigneeUserId(),
//                BpmMessageEnum.TASK_ASSIGNED.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenTaskTimeout(BpmMessageSendWhenTaskTimeoutReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("taskName", reqDTO.getTaskName());
//        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
//        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getAssigneeUserId(),
//                BpmMessageEnum.TASK_TIMEOUT.getSmsTemplateCode(), templateParams));
    }


}
